package com.ourhealth.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ourhealth.mapper.OrderSettingMapper;
import com.ourhealth.pojo.OrderSetting;
import com.ourhealth.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author dddddh
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional(rollbackFor = Exception.class)
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    OrderSettingMapper orderSettingMapper;

    /**
     * 新建或修改预约人数上限
     * @param orderSetting
     */
    @Override
    public void handleOrderSet(OrderSetting orderSetting) {
        int count = isHandleOrderSet(orderSetting.getOrderDate());
        if (count!=0){
            updateHandleOrderSet(orderSetting);
        }else {
            addHandleOrderSet(orderSetting);
        }
    }

    /**
     * 判断是否已经生成该日期的预约人数上限
     * @return
     * @param date
     */
    @Override
    public int isHandleOrderSet(Date date) {
        return orderSettingMapper.isHandleOrderSet(date);
    }

    /**
     * 更新该日期的预约人数上限
     *@param orderSetting
     */
    @Override
    public void updateHandleOrderSet(OrderSetting orderSetting) {
        orderSettingMapper.updateHandleOrderSet(orderSetting);
    }

    /**
     * 添加该日期的预约人数上限
     *@param orderSetting
     */
    @Override
    public void addHandleOrderSet(OrderSetting orderSetting) {
        orderSettingMapper.addHandleOrderSet(orderSetting);
    }

    /**
     * 通过月份获取预约详情
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //2019-3-1
        String dateBegin = date + "-1";

        //2019-3-31
        String dateEnd = date + "-31";
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> list = orderSettingMapper.getOrderSetting(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            //获得日期（几号）
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());
            //获得日期（几月）
            orderSettingMap.put("month",orderSetting.getOrderDate().getMonth());
            //可预约人数
            orderSettingMap.put("number",orderSetting.getNumber());
            //已预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());
            data.add(orderSettingMap);
        }
        return data;
    }

    /**
     * 批量导入设置
     * @param orderSettingList
     */
    @Override
    public void addOrderSettingList(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            if (orderSetting.getOrderDate()!=null){
                handleOrderSet(orderSetting);
            }
        }
    }
}
